<template>
	<view :style="theme" style="background-color: var(--bg);color:var(--color);height: 100vh;">
		 <button @tap="open">打开数据库</button>
		  <button @tap="close">关闭数据库</button>
		 <button @tap="read">读取数据库</button>
		 <button @tap="add">新增数据表</button>
		  <button @tap="addData">新增数据</button>
		  <button @tap="clearTable">清空数据表</button>
		  <button @tap="createSnowId">创建id</button>
		  <button @tap="changeTheme">切换主题</button>
	</view>
</template>

<script>
	import Sqlite from '@/library/sqlite/sqlite.main.js';
	import SnowflakeID from '@/library/snowID/snowFlakeID.js';
	export default{
		data(){
			return{
				SnowflakeIDInstance:null
			}
		},
		onLoad(){
			// console.log(this.SnowflakeIDInstance);
		   this.SnowflakeIDInstance = new SnowflakeID ({mid:new Date()})
		  console.log(this)
		},
		methods:{
			async open(){
				console.log("数据库是否开启:"+Sqlite.IsOpen())
				if(!Sqlite.IsOpen()){
					Sqlite.OpenSqlite().then(e=>{
						console.log(e);
					}).catch(e=>{
						console.log(e)
					})
					
				}
			},
			close(){
			   Sqlite.CloseSqlite();	
			},
			async read(){
				if(Sqlite.IsOpen()){
					let has = await Sqlite.QueryIsTable("test");
					console.log(has);
					if(!has)return;
					let res = await Sqlite.QueryCount("test");
					console.log(res);
					
					Sqlite.SelectDataList("test")
					.then(e=>{
						console.log(e);
					})
					.catch(e=>console.log(e))
				} 
			},
			async add(){
				if(Sqlite.IsOpen()){
				Sqlite.CreateTable("test",{id:"",name:"",data:{}})
				// Sqlite.CreateTable("test","'id' varchar(50),'name' TEXT,'data' TEXT")
				.then(e=>console.log(e))
				.catch(e=>console.log(e))
				} 
			},
			addData(){
				if(Sqlite.IsOpen()){
					// Sqlite.createTable("test","'id' varchar(50),'name' TEXT,'data' TEXT")
					// .then(e=>console.log(e))
					// .catch(e=>console.log(e))
					var d = {
						num:20,
						age:33,
						address:'2fadfadfjajfadfaf'
					}
					var data = `'${this.SnowflakeIDInstance.Generate()}','hhhh','${JSON.stringify(d)}'`
					Sqlite.InsertTableData("test",data)
					.then(e=>console.log(e))
					.catch(e=>console.log(e))
				}
			},
			clearTable(){
				 Sqlite.DropTable('test');
				// Sqlite.DeleteTableRow('test',1);
			},
			createSnowId(){
				console.log(this.SnowflakeIDInstance.Generate());
			},
			changeTheme(){
				this.themeName=='light'?this.$store.commit("setTheme", 'dark'):this.$store.commit("setTheme", 'light')
			}
		}
	}
</script>

<style>
	button {
		color:var(--bg);
	}
</style>